package com.bw.module_service.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bw.module_service.R
import com.bw.module_service.databinding.ItemServiceBinding
import com.bw.module_service.model.ServiceModel

class ServiceAdapter:RecyclerView.Adapter<ServiceViewHolder>() {

    private val list = mutableListOf<ServiceModel>()

    var onItemClick:((ServiceModel) -> Unit)? = null

    fun setData(data:List<ServiceModel>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServiceViewHolder {
        val binding = DataBindingUtil.inflate<ItemServiceBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_service,
            parent,
            false
        )
        return ServiceViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ServiceViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemServiceBinding>(holder.itemView)?:return
        binding.serviceImg.setImageResource(list[position].img)
        binding.serviceName.text = list[position].title
        Log.d("zty","onBindViewHolder:"+list[position].title)

        holder.itemView.setOnClickListener {
            onItemClick?.invoke(list[position])
        }
    }

    override fun getItemCount(): Int {
        return list.size
        Log.d("zty","getItemCount: ${list.size}")
    }

}

class ServiceViewHolder(val binding: ItemServiceBinding):RecyclerView.ViewHolder(binding.root)